﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BonsaiScrum.Core.Domain.Shared;

namespace BonsaiScrum.Core.Domain
{
    public class Task : BaseEntity<Task>
    {
        private TaskStatus _status;
        private Priority _priority;
        private int _estimatedHoursToComplete;
        private IList<User> _assignedTo;

        private string _description;

        protected Task() 
        {
            _assignedTo = new List<User>();
        }

        public Task(string description, Priority priority, int estimatedHoursToComplete) : this()
        {
            if (string.IsNullOrEmpty(description))
                throw new ArgumentNullException("description");

            _description = description;
            _priority = priority ?? Priority.GetItDoneByNextRelease;
            _estimatedHoursToComplete = estimatedHoursToComplete;
            _status = TaskStatus.Unclaimed;
        }
        
        public virtual bool IsCompleted()
        {
            return _status.Equals(TaskStatus.Completed);
        }

        public virtual void Complete()
        {
            _status = TaskStatus.Completed;
        }

        public virtual bool HasBeenAssigned()
        {
            return _assignedTo.Count > 0;
        }

        public virtual void AssignTo(IRole user)
        {
            if (user == null)
                throw new ArgumentNullException("user");

            _assignedTo.Add(user.Actor);

            user.Actor.AddTask(this);
        }

        public override string ToString()
        {
            return string.Format("Task: '{0}'", _description);
        }
    }
}
